<!--  -->
<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>评价管理</el-breadcrumb-item>
      <el-breadcrumb-item>评价列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区 -->
    <el-card>
      <!-- 警告区域 -->
      <el-alert title="注意：请先选择商品分类！" type="warning" show-icon :closable="false"></el-alert>

      <!-- 选择商品分类区域 -->
      <el-row class="cat_opt">
        <el-col :span="8">
          <span>选择商品分类：</span>
          <!-- 商品分类的级联选择框 -->
          <el-cascader
            v-model="selectedCateKeys"
            :options="cateList"
            :props="cateProps"
             @change="handleChange"
          ></el-cascader>
        </el-col>
        <el-col :span="12">
          <div class="text item">
             <span>选择商品：</span>
            <el-select v-model="selectProid" placeholder="请选择"  @change="getProid">
              <el-option
                v-for="item in goodsList"
                :key="item.id"
                :label="item.spuName"
                :value="item.id"
               >
              </el-option>
            </el-select>
            </div
        >
        </el-col>
      </el-row>
      <!-- 评价列表区 -->
      <el-table :data="commentInfo" highlight-current-row border>
        <el-table-column type="index" width="40"> </el-table-column>
        <el-table-column property="commentType" label="评价等级" width="90">
          <template slot-scope="scope">
            <el-tag v-show="scope.row.star>=3">好评</el-tag>
            <el-tag v-show="scope.row.star<=2" type="danger">差评</el-tag>
        </template>

        </el-table-column>
        <el-table-column label="评价内容" property="description">
          <template slot-scope="scope">
            <div class="description">{{ scope.row.description }}</div>
          </template>
        </el-table-column>
        <el-table-column property="star" label="评价星级" width="100">
        </el-table-column>
        <el-table-column property="likesCount" label="点赞量" width="100"> </el-table-column>
        <el-table-column property="replyCount" label="回复" width="140">
          <template slot-scope="scope">
            <el-tag v-show="scope.row.replyCount>=1">已回复</el-tag>
            <el-tag v-show="scope.row.replyCount<=0" type="danger">未回复</el-tag>
          </template>
           </el-table-column>
        <el-table-column property="createTime" label="评价时间" width="180"> 
          <template slot-scope="scope">
           {{scope.row.createTime | dataFormat}}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="100">
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              @click="showEditDialog(scope.row)"
              >回复</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pageNum"
        :page-sizes="[2, 3, 5]"
        :page-size="queryInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <!-- 回复评价对话框 -->
    <el-dialog
      title="回复内容"
      width="50%"
      :visible.sync="dialogEditVisible"
      @close="editDialogClose"
    >
      <el-form
        :model="updateCommentInfo"
        ref="editFormRef"
        label-width="100px"
        class="demo-ruleForm"
      >
      <el-form-item label="评价内容" prop="description">
        <el-input v-model="updateCommentInfo.description" disabled></el-input>
        </el-form-item>
        <el-form-item label="回复消息" prop="replyInfo">
          <textarea
            v-model="updateCommentInfo.replyInfo"
            class="updateCommentContent"
          ></textarea>
        </el-form-item> 
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogEditVisible = false">取 消</el-button>
        <el-button type="primary" @click="updataComment">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "Comment",
  data() {
    return {
      // 商品分类列表
      cateList: [],
      //   级联选择框的配置对象
      cateProps: {
        expandTrigger: 'hover',
        value:"catId",
        label:'catName',
        children:"children"
      },
      //   级联选择框双向绑定对象
      selectedCateKeys: [],
      selectProid:'',
      queryInfo: {
        query: "",
        // 当前页码
        pageNum: 1,
        // 当前每页显示多少条数据
        pageSize: 2,
      },
      dialogEditVisible: false,
      total: 10,
      // spu商品信息
      goodsList:[],
      // 评价信息
      commentInfo: [],
      // 修改信息
      updateCommentInfo: {
        description:'',
        replyInfo:''
      },
      updataInfo:{}
    };
  },
  created() {
    // this.getCommentList();
     this.getCateList()
  },
  computed:{
     // 当前选中的二级分类Id
    getCateId () {
      if (this.selectedCateKeys.length === 2) {
        return this.selectedCateKeys[1]
      }
      return null
    },
  },
  methods: {
    // 根据获取对应的商品列表
    async getGoodsList () {
      const {data:res} = await this.$http.get(`/xuanmall/spumessage/list?publishStatus=${this.getCateId}`)
      console.log(res)
      this.goodsList =[],
       res.page.forEach(value =>{
         var data = {};
         data.id = value.id
         data.spuName = value.spuName
         data.images = value.images[0]
         data.price = value.price
         data.weight = value.weight
         data.stockCount = value.stockCount
         data.publishStatus = value.publishStatus
         this.goodsList.push(data)
       })
       console.log(this.goodsList)
    },
    //   获取所有的商品分类列表
    async getCateList () {
      const {data:res} = await this.$http.get("/xuanmall/category/list/tree")
        if(res.code === 0){
         this.cateList = this.formatData(res.data)
        }
    },
    // 格式化数据，递归将空的children置为undefined
    formatData(data) {
      for (var i = 0; i < data.length; i++) {
        if (data[i].children.length < 1) {
          data[i].children = undefined
        } else {
          this.formatData(data[i].children)
        }
      }
      return data
      },
    getProid(){
      this.getCommentList();
    },
    // 级联选择框 选中变化 触发
    async  handleChange () {
      this.getGoodsList()
    },
    // 监听pageSize改变
    handleSizeChange(newSize) {
      // console.log(newSize)
      this.queryInfo.pageSize = newSize;
      this.getCommentList();
    },
    // 监听页码值改变事件
    handleCurrentChange(newPage) {
      // console.log(newPage)
      this.queryInfo.pageNum = newPage;
      this.getCommentList();
    },
    // 获取评价列表
   async getCommentList() {
      console.log(this.selectProid)
     const {data:res} = await this.$http.get(`/xuanmall/shopcomment/getComment/${this.selectProid}?page=${this.queryInfo.pageNum}&limit=${this.queryInfo.pageSize}`)
    //  console.log(res)
     if(res.code === 0){
       this.commentInfo = res.page.list
       this.total = res.page.totalCount
     }
     console.log(this.commentInfo)
   },
    // 回复评价信息
    async updataComment() {
      this.updataInfo.description=this.updateCommentInfo.replyInfo
      const {data:res} = await this.$http.post("/xuanmall/shopcomment/save",this.updataInfo)
      console.log(res)
      if(res.code !== 0){
        this.$message.error("回复失败")
      }
      this.$message.success("回复成功")
      this.dialogEditVisible = false;
    },
    // 修改对话框显示
   async showEditDialog(row) {
      this.updataInfo.spuId = row.spuId
      this.updataInfo.skuId = row.skuId
      this.updataInfo.parentId = row.id
      this.updataInfo.commentType = 1
      this.updataInfo.description = ''
      if(row.replyCount){
        const {data:res} = await this.$http.get(`/xuanmall/shopcomment/info/${row.id}`)
        // console.log(res)
        this.updateCommentInfo.replyInfo =res.shopCommentEntities[res.shopCommentEntities.length-1].description
      }
      this.dialogEditVisible = true;
      this.updateCommentInfo.description = row.description;

    },
    // 关闭修改对话框
    editDialogClose() {
      this.$refs.editFormRef.resetFields();
    },
    
  },
};
</script>
<style lang="less" scoped>
.headimag {
  height: 90px;
  width: 90px;
}
.updateCommentContent {
  width: 530px;
}
.description,
.commentTitle {
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}
</style>
